<?php

if(!defined('IN_SITE')) {
	exit('Access Denied');
}

class system_grade{
	public static $tree2array=array();
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();

	static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}

	/*
	*	返回子集部门树形数组
	*/
	public function get_sub_tree($d_id) {
		//取出根单位编号
		$d_root_id_sql = "SELECT d_root_id FROM ".DB::table('dictionary')." WHERE isdelete=0 AND d_id='".$d_id."' LIMIT 1";
		$d_root_id_sql_result = DB::result_first($d_root_id_sql);
		//DEBUG 取出子单位编号
		$d_id_sql = "SELECT d_id,d_level,d_name,d_parent_id,d_recommend,d_recommend_order FROM ".DB::table('dictionary')." WHERE isdelete=0 AND d_root_id='".$d_root_id_sql_result."' ORDER BY d_order ASC, d_id ASC, d_recommend_order ASC";
		$d_id_sql_result = DB::fetch_all($d_id_sql);
		$d_id_array = self::dbarr2tree_general($d_id_sql_result, $d_id);
		return $d_id_array;
	}

    /*
    * 通用树形递归取出所有子节点(带层级)
    */
    public function dbarr2tree_general($tree, $root_id,$d_id_name='d_id',$d_parent_id_name='d_parent_id',$children_name='children') {
        $return = array();
        foreach($tree as $leaf) {
            if($leaf[$d_parent_id_name] == $root_id) {
                foreach($tree as $subleaf) {
                    if($subleaf[$d_parent_id_name] == $leaf[$d_id_name]) {
                        $leaf[$children_name] = self::dbarr2tree_general($tree, $leaf[$d_id_name],$d_id_name='d_id',$d_parent_id_name='d_parent_id',$children_name='children');
                        break;
                    }
                }
                $return[] = $leaf;
            }
        }
        return $return;
    }
	/*
	*	返回子集部门一维数组
	*/
	public static function tree2array($tree){
		//print_r($tree);
		foreach($tree as $leaf) {
			$children = $leaf['children'];
			unset($leaf['children']);
			if($leaf['d_id']){
				self::$tree2array[$leaf['d_id']]= $leaf;
			}
			if(!empty($children)) {
				self::tree2array($children);
			}
		}
		return self::$tree2array;
	}

	/*
	*	根据前段样式要求返回子集部门下拉框列表信息
	*/
	public function tree2option($tree,$indent = '') {
		$tree2htmlul .= '';
		$indent .= '&nbsp;&nbsp;';
		foreach($tree as $leaf) {
			$tree2htmlul .= '<option value="'.$leaf['d_id'].'" title="'.$leaf['d_name'].'">'.$indent.$leaf['d_name']."</option>";
			if(!empty($leaf['children'])) {
				$tree2htmlul .= self::tree2option($leaf['children'],$indent);
			}
			$tree2htmlul .= '';
		}
		$tree2htmlul .= '';
		return $tree2htmlul;
	}

	/*
	* 返回子集部门下拉框
	*/
	public function get_user_sub_tree2option(){
		global $_G;
		$d_id = !empty($_G['member']['dept_id']) ? $_G['member']['dept_id']:300;
		$d_id_array = $this->get_sub_tree($d_id);
		$user_sub_tree2option = $this->tree2option($d_id_array);
		if($d_id !== 300){
			$user_top_option = $this->get_info($d_id);
			$user_sub_tree2option = '<option value="'.$user_top_option['d_id'].'" title="'.$user_top_option['d_name'].'">'.$user_top_option['d_name']."</option>".$user_sub_tree2option;
		}
		return $user_sub_tree2option;
	}

	/*
	* 返回根据dept_id返回下级机构
	*/
	public function get_sub_option(){
		global $_G;
		$sub_option = '';
		$d_id = !empty($_GET['dept_id']) ? $_GET['dept_id']:'';
		if($d_id){
			$d_id_sql = "SELECT d_id,d_name FROM ".DB::table('dictionary')." WHERE isdelete=0 AND d_parent_id='".$d_id."'";
			$d_id_sql_result = DB::fetch_all($d_id_sql);
			if($d_id_sql_result){
				foreach($d_id_sql_result AS $key => $value){
					$sub_option .= '<option value="'.$value['d_id'].'" title="'.$value['d_name'].'">'.$value['d_name']."</option>";
				}
			}
		}
		return $sub_option;
	}

	/*
	* 取出一条部门信息
	*/
	public function get_info($d_id) {
		$info_array = array();
		if($d_id){
			$info_sql = "SELECT * FROM ".DB::table('dictionary')." WHERE d_id='".$d_id."' LIMIT 1"; 
			$info_array = DB::fetch_first($info_sql);
		}
		return $info_array;
	}

	/*
	* 取出多条部门信息
	*/
	public function get_multi_info($d_ids) {
		$info_array = array();
		if($d_ids && is_array($d_ids)){
			$sql = "SELECT d_id,d_name FROM ".DB::table('dictionary')." WHERE d_id IN (".dimplode($d_ids).")"; 
			$result = DB::fetch_all($sql);
			foreach($result AS $key => $value){
				$info_array[$value['d_id']]=$value['d_name'];
			}
		}
		return $info_array;
	}

	/*
	* 递归取出用户所在部门路径 默认最大层级深度20级
	*/
	public function get_user_depts($user_dept_id,$depth_level=20){
		$depts_array = array();
		if($user_dept_id){
			//取出根部门
			for($i=1; $i<=$depth_level; $i++){
				$sql = "SELECT d_level, d_parent_id, d_name FROM ".DB::table('dictionary')." WHERE isdelete=0 AND d_id='".$user_dept_id."' LIMIT 1";
				$result = DB::fetch_first($sql);
				$depts_array[$result['d_level']]=$result['d_name'];
				$user_dept_id = $result['d_parent_id'];
				if(1==$result['d_level']){
					break;
				}
			}
		}
		if($depts_array){
			ksort($depts_array);
		}
		return $depts_array;
	}

	/*
	* ajax 返回信息
	*/
	public function return_json($error='true',$message='操作失败') {
		$return_array = array(
			"error" => $error,
			"message" => $message
		);
		echo json_ext($return_array);
		die;
	}
}

?>